package com.heima.kafka.contrller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @RequestMapping("/sendMsg")
    public String sendMsg(){

        kafkaTemplate.send("topic", " springboot kafka");

        return "success";
    }

    @RequestMapping("/sendObj")
    public String sendObj(){

        User user = new User();
        user.setUsername("xiaowang");
        user.setAge(18);

        kafkaTemplate.send("topic", JSON.toJSONString(user));

        return "success";
    }
}
